<?php

namespace app\common\command\addon;

use app\common\library\addon\Addon;
use think\console\Command;
use think\console\input\Option;

class AddonsCommand extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('addons:action')
            ->addOption('addon','d',Option::VALUE_OPTIONAL,'插件名')
            ->addOption('action','a',Option::VALUE_REQUIRED,'插件方法')
            ->addOption('file_path','f',Option::VALUE_REQUIRED,'文件所在路径')
            ->setDescription('插件方法');
    }
    protected function execute(\think\console\Input $input, \think\console\Output $output)
    {
//        echo 111;
        $addon = $input->getOption('addon');
        $action = $input->getOption('action');
        if($action=='install'){
            $file_path = $input->getOption('file_path');
            if(!$file_path){
                $output->writeln('请输入文件路径');
                return;
            }
            if(!file_exists($file_path)){
                $file_path = root_path().$file_path;
            }
            Addon::install($file_path);
        }else{
            if(!$addon || !$action){
                $output->writeln('请输入插件名和方法');
                return;
            }
            if($action=='upgrade'){
                $file_path = $input->getOption('file_path');
                if(!$file_path){
                    $output->writeln('请输入文件路径');
                    return;
                }
                if(!is_file($file_path)){
                    $file_path = root_path().$file_path;
                }
                Addon::upgrade($addon,$file_path);
                return;
            }
            Addon::$action($addon);
        }
        // 指令输出
//        $output->writeln('Hello Command');
    }
}